package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {

        User byUsername = userDao.findByUsername(username);

        if (byUsername == null) {
            return null;
        }

        Set<Role> roles = roleDao.findByUserId(byUsername.getId());

        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {

                Set<Permission> permissions = permissionDao.findByRoleId(role.getId());

                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
            }
            byUsername.setRoles(roles);
        }

        return byUsername;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件

        if (queryString != null && queryString.length() > 0) {
            queryString = "%" + queryString + "%";
        }

        //完成分页查询，基于mybatis框架提供的分页助手插件完成,自动返回total封装进page
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<User> rows = page.getResult();

        return new PageResult(total, rows);
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        setUserAndRole(user.getId(), roleIds);

    }

    @Override
    public void del(Integer user_id) {
        userDao.deleteAssociation(user_id);
        userDao.del(user_id);
    }

    @Override
    public User findById(Integer user_id) {
        return userDao.findById(user_id);
    }

    @Override
    public List<Integer> findRolesIdsByUserId(Integer user_id) {
        return userDao.findRolesIdsByUserId(user_id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.deleteAssociation(user.getId());
        setUserAndRole(user.getId(),roleIds);
        userDao.edit(user);
    }




    public void setUserAndRole(Integer user_id, Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0) {
            for (Integer role_id : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", user_id);
                map.put("role_id", role_id);
                userDao.setUserAndRole(map);
            }
        }
    }


}
